var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function compareV(t, a) {
  if (t && a) {
      for (var e = t.split("."), o = a.split("."), n = Math.min(e.length, o.length), i = 0, s = 0; i < n && 0 == (s = parseInt(e[i]) - parseInt(o[i])); ) i++;
      return 0 <= (s = 0 != s ? s : e.length - o.length);
  }
  return console.log("版本号不能为空"), !1;
}

var that, app = getApp();
let interstitialAd = null;
var videoAd = null;

Page({
    data: {
        TabCur:0,  //当前选中标题的下标
        scrollLeft:0,    //tab滚动条的位置
        saveCode:"",
        // MemberIntegral:0,
        show_single: !0,
    },
    onLoad: function(f) {
      f.parent_id && wx.setStorageSync("parent_id", f.parent_id), that = this, compareV(wx.getSystemInfoSync().SDKVersion, "2.10.4") || (console.log(2121), 
      this.setData({
          show_single: !1
      }));

      that = this;
      wx.setNavigationBarColor({
          frontColor: "#000000",
          backgroundColor: "#000000"
      });
      var t = {
          action: "group",
          contr: "index",
          token: wx.getStorageSync("token")
      };
      _tools2.default.request(t, function(t) {
          that.setData(t.info,'groupInfo:t.info');
          that.setData({
            TabCur:t.info.group_type[0].id
          });
          // console.log(t.info);
          
      });

        // 激励视频
        // if (wx.createRewardedVideoAd) {
        //   // 加载激励视频广告
        //   videoAd = wx.createRewardedVideoAd({
        //     adUnitId: 'adunit-b3ebfdf476915c40'
        //   })
        //   //捕捉错误
        //   videoAd.onError(err => {
        //   // 进行适当的提示
        //   })
        //   // 监听关闭
        //   videoAd.onClose((status) => {
        //     if (status && status.isEnded || status === undefined) {
        //       // 正常播放结束，下发奖励
        //       // continue you code
        //       that.saveImage()
        //     } else {
        //       // 播放中途退出，进行提示
        //     }
        //   })
        // }
    },



    onReady: function() {

    },



    onShow: function() {
      console.log(this.data)
      var t = {
            action: "index",
            contr: "my",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        }), wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        });

      // this.receive(a)

        if (wx.createInterstitialAd) {
            interstitialAd = wx.createInterstitialAd({
              adUnitId: 'adunit-42e141bfcfdcc315'
            })
            interstitialAd.onLoad(() => {})
            interstitialAd.onError((err) => {})
            interstitialAd.onClose(() => {})
          };
          
          // 在适合的场景显示插屏广告
          if (interstitialAd) {
            interstitialAd.show().catch((err) => {
              console.error(err)
            })
          };
    },

    // button 点击事件
    openVideoAd(tt) {
      that = this;

      wx.showModal({
        title: '进群提示',
        mask:!0,
        content:'即将扣除积分并下载群二维码',
        showCancel:true,
        success(res){
          if (res.confirm) {
            //扣除积分
              var t = {
                  action: "get_group",
                  contr: "index",
                  token: wx.getStorageSync("token"),
              };
              _tools2.default.request(t, function(t) {
                    let MemberIntegral = t.info.code;
                    console.log(MemberIntegral);
                    if(MemberIntegral !== -999){
                        // 激励视频
                      that.saveCode = tt.currentTarget.dataset.code;
                      that.saveImage();
                      // console.log('打开激励视频');
                      // // 在合适的位置打开广告
                      // if (videoAd) {
                      //   videoAd.show().catch(err => {
                      //     // 失败重试
                      //     videoAd.load()
                      //       .then(() => videoAd.show())
                      //   })
                      // }
                    } else{
                      wx.showModal({
                        title: '温馨提示',
                        mask:!0,
                        content:t.info.msg,
                        showCancel:true,
                        success(){
                          wx.switchTab({
                            url: '/bh_bargain/pages/signIn/signIn',   
                          })
                        }
                      })              
                      // return;
                    }                       
              });
          } else if (res.cancel) {
            console.log('取消')
            return;
          }
        }
      }) 
    },

    saveImage: function() {
        let that = this;
        // var e = t.currentTarget.dataset.code;
        wx.downloadFile({
            url: that.saveCode,
            success: function(t) {
                200 === t.statusCode && wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        wx.showToast({
                            title: "保存成功"
                        });
                    }
                });
            }
        });
    },

      tabSelect(e) {
        var type_id = e.currentTarget.dataset.id;
        var t = {
          action: "group",
          contr: "index",
          token: wx.getStorageSync("token"),
          type: type_id
      };
      _tools2.default.request(t, function(t) {
          that.setData(t.info);
      });
      console.log(e);
        this.setData({
          TabCur: e.currentTarget.dataset.id,
          scrollLeft: (e.currentTarget.dataset.id-1)*30
        })
      },


  receive: function(t) {
    var a = {
        action: "receive",
        contr: "sign",
        id: t,
        token: wx.getStorageSync("token")
    };
    1 == this.data.is_request && (this.data.is_request = 2, _tools2.default.request(a, function(t) {
        that.loadSuspension(), that.data.is_request = 1, wx.showToast({
            title: "领取奖励成功"
        });
    }));
  },

  loadSuspension: function() {
    var t = {
        action: "suspension",
        contr: "index",
        token: wx.getStorageSync("token")
    };
    _tools2.default.request(t, function(t) {
        that.setData(t.info);
    });
  },
  clickSuspension: function(t) {
      var e = t.currentTarget.dataset.index;
      1 == this.data.task[e].type ? wx.navigateTo({
          url: "/bh_bargain/pages/luck/luck"
      }) : 2 == this.data.task[e].type ? this.videoAdShow(this.data.task[e].id) : 3 == this.data.task[e].type && wx.navigateToMiniProgram({
          appId: that.data.task[e].appid,
          path: that.data.task[e].path,
          extraData: {},
          envVersion: "release",
          success: function(t) {
              var a = {
                  id: that.data.task[e].id,
                  demo_time: that.data.task[e].number,
                  time: Date.parse(new Date())
              };
              wx.setStorageSync("task", a);
          },
          fail: function(t) {}
      });
    },
      
    onShareAppMessage: function() {
        return {
            title: this.data.share.text,
            imageUrl: this.data.share.images,
            path: "bh_bargain/pages/group/group?parent_id=" + this.data.share.member_id
        };
    },
    onShareTimeline:function(res){
      let _this = this;
      return{
        title:this.data.share.text,
        imageUrl: this.data.share.images,
        path: "bh_bargain/pages/group/group?parent_id=" + this.data.share.member_id
      }
     },

    // onShareAppMessage: function (res) {
    //     let gbid = res.target.dataset.info.order_id;
    //     return {
    //       title: '分享',
    //       path: '/pages/imgroup/imgroup?tgid=' + gbid,
    //       imageUrl: 'https://wq.milugou.cn/attachment/images/2/2021/01/QlcD0XV8d60erYfr22BZ26aRvFDfBR.png',  //用户分享出去的自定义图片大小为5:4,
    //       success: function (res) {
    //        // 转发成功
    //             wx.showToast({
    //               title: "分享成功",
    //               icon: 'success',
    //               duration: 2000
    //             })
    //        },
    //       fail: function (res) {
    //         // 分享失败
    //       },
    //     }
    //   },
    getUserInfo: function(t) {
      console.log("getUserInfo")
      if ("getUserInfo:ok" == t.detail.errMsg) {
          var e = {
              action: "register",
              contr: "my",
              token: wx.getStorageSync("token"),
              encryptedData: t.detail.encryptedData,
              iv: t.detail.iv
          };
          _tools2.default.request(e, function(t) {
              that.onShow();
          });
      }
  }
});